package cup.com.icss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.dao.DormitoryMapper;
import cup.com.icss.dao.MailSystemMapper;
import cup.com.icss.dao.UserMapper;
import cup.com.icss.dao.WaterElectricityMapper;
import cup.com.icss.model.Dormitory;
import cup.com.icss.model.MailSystem;
import cup.com.icss.model.User;
import cup.com.icss.model.WaterElectricity;
import cup.com.icss.utils.LogUtil;
import cup.com.icss.utils.StringUtil;

@Controller
public class waterElectrictyController {
	@Resource
	private WaterElectricityMapper waterElectricityMapper;
	@Resource
	private DormitoryMapper dormitoryMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private MailSystemMapper mailSystemMapper;
	
	@RequestMapping("/waterElectrictyInitialize")
	@ResponseBody
	public ModelAndView waterElectrictyInitialize(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("roleName",user.getRoleName());
			view.addObject("storiedBuilding",user.getStoriedBuilding());
			view.addObject("studentId",user.getStudentId());
			view.setViewName("cup/jsp/waterElectricity/waterElectricityInitialize");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/waterElectricityList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> waterElectricityList(HttpServletRequest request, Integer page, Integer rows, WaterElectricity record, String roleName, String studentId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			page=page==null?1:page;
			rows=rows==null?10:rows;
			int pageNum = page;
			int pageSize = rows;
			if(roleName.equals("ADMINISTRATOR")){
				int num = waterElectricityMapper.waterElectricityListSize(record).size();
				int pageSizez;
				if(((pageSize*pageNum)-num)<0){
					pageSizez = pageSize;
				}else{
					pageSizez = num-(10*(pageNum-1));
				}
				List<WaterElectricity> somethingLost = waterElectricityMapper.waterElectricityList(pageNum*pageSize-pageSize,pageSizez,record);
				map.put("total", num);
				map.put("rows", somethingLost);
			}else{
				Dormitory dormitory = dormitoryMapper.selectStudentIdObj(studentId);
				record.setDormitory(dormitory.getDormitory());
				int num = waterElectricityMapper.waterElectricityListSize(record).size();
				int pageSizez;
				if(((pageSize*pageNum)-num)<0){
					pageSizez = pageSize;
				}else{
					pageSizez = num-(10*(pageNum-1));
				}
				List<WaterElectricity> somethingLost = waterElectricityMapper.waterElectricityList(pageNum*pageSize-pageSize,pageSizez,record);
				map.put("total", num);
				map.put("rows", somethingLost);
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/waterElectricityAddInitialize",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView waterElectricityAddInitialize(HttpServletRequest request, String storiedBuilding){
		ModelAndView view = new ModelAndView();
		try {
			view.addObject("storiedBuilding",storiedBuilding);
			view.setViewName("cup/jsp/waterElectricity/waterElectricityAdd");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/waterElectricityAddData",method=RequestMethod.POST)
	@ResponseBody          
	public Map<String, String> waterElectricityAddData(HttpServletRequest request,WaterElectricity record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			record.setId(StringUtil.getUUID());
			int inster = waterElectricityMapper.insertSelective(record);
			if(inster>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/waterElectricityEditInitialize",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView waterElectricityEditInitialize(HttpServletRequest request,String id){
		ModelAndView view = new ModelAndView();
		try {
			WaterElectricity record = waterElectricityMapper.selectByPrimaryKey(id);
			view.addObject("record",record);
			view.setViewName("cup/jsp/waterElectricity/waterElectricityEdit");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/waterElectricityEditData",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> waterElectricityEditData(HttpServletRequest request,WaterElectricity record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int update = waterElectricityMapper.updateByPrimaryKeySelective(record);
			if(update>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/waterElectricityDel",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> waterElectricityDel(HttpServletRequest request,String id){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int temp = 0;
			List<String> Id = Arrays.asList(id.split(","));
			for(int i=0; i<Id.size(); i++){
				if(waterElectricityMapper.deleteByPrimaryKey(Id.get(i))==1){
					temp++;
				}
			}
			if(temp == Id.size()){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/waterElectricityMail",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> waterElectricityMail(HttpServletRequest request,String id){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int temp = 0;
			int temp1 = 0;
			Date time = new Date();
			User user = (User)request.getSession().getAttribute("USER");
			List<String> Id = Arrays.asList(id.split(","));
			MailSystem mailSystem = new MailSystem();
			mailSystem.setfCnName(user.getCnName());
			mailSystem.setfStudentId(user.getStudentId());
			mailSystem.setfCollege(user.getCollege());
			mailSystem.setfTheClass(user.getTheClass());
			mailSystem.setStatus("1");
			mailSystem.setTime(time);
			for(int i=0;i<Id.size();i++){
				WaterElectricity waterElectricity =  waterElectricityMapper.selectByPrimaryKey(Id.get(i));
				mailSystem.setTitle(waterElectricity.getYears()+"年"+waterElectricity.getMonth()+"月"+"水电费缴费提醒");
				mailSystem.setContent("用水量："+waterElectricity.getWater()+"；单价(水/升)："+
				waterElectricity.getWaterPrice()+"-|-用电量："+waterElectricity.getElectricity()+
				"；单价(电/度)："+waterElectricity.getElectricityPrice()+"---合计："+waterElectricity.getCombined());
				String storiedBuilding = waterElectricity.getStoriedBuilding();
				String dormitory = waterElectricity.getDormitory();
				List<Dormitory> persons = dormitoryMapper.selectDormitoryPersons(storiedBuilding, dormitory);
				if(persons.size()>=1){											 
					for(int j=0;j<persons.size();j++){
						Dormitory person = persons.get(j);
						String studentId = person.getStudentId();
						User per = userMapper.selectByPrimaryKey(studentId);
						mailSystem.setId(StringUtil.getUUID());
						mailSystem.setjCnName(per.getCnName());
						mailSystem.setjStudentId(per.getStudentId());
						mailSystem.setjCollege(per.getCollege());
						mailSystem.setjTheClass(per.getTheClass());
						if(mailSystemMapper.insertSelective(mailSystem)==1){
							temp++;
						}
					}
				}
			}
			map.put("status", "200");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
}
